class Store {
  type: Storage;
  constructor(type: Storage) {
    this.type = type;
  }

  /**
   * 获取值
   * @param key 键名 string | number
   * @returns 处理后的值 any
   */
  get(key: string | number) {
    let value: unknown = this.type.getItem('' + key);
    try {
      value = JSON.parse('' + value);
    } catch (err) {
      console.error(err);
    }
    return value;
  }

  /**
   * 设置值
   * @param key 键名 string | number
   * @param value 存入值 any
   */
  set(key: string | number, value: unknown) {
    let val: string;
    try {
      val = JSON.stringify(value);
    } catch (err) {
      val = '';
    }
    this.type.setItem('' + key, val);
  }

  /**
   * 清除store
   */
  clear() {
    this.type.clear();
  }

  /**
   * 删除某值
   * @param key key 键名 string | number
   */
  remove(key: string | number) {
    this.type.removeItem('' + key);
  }
}

const lstore = new Store(window.localStorage);
const sstore = new Store(window.sessionStorage);

export { lstore, sstore };
